package cn.iocoder.yudao.module.system.service.erp.warehouseinsku;
import cn.iocoder.yudao.module.system.controller.admin.erp.warehouseinsku.vo.WarehouseInSkuPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.erp.warehouseinsku.vo.WarehouseInSkuSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.erp.warehouseinsku.WarehouseInSkuDO;
import cn.iocoder.yudao.module.system.dal.mysql.erp.warehouseinsku.WarehouseInSkuMapper;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.WAREHOUSE_IN_SKU_NOT_EXISTS;

/**
 * 入库单sku Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class WarehouseInSkuServiceImpl implements WarehouseInSkuService {

    @Resource
    private WarehouseInSkuMapper warehouseInSkuMapper;

    @Override
    public Long createWarehouseInSku(WarehouseInSkuSaveReqVO createReqVO) {
        // 插入
        WarehouseInSkuDO warehouseInSku = BeanUtils.toBean(createReqVO, WarehouseInSkuDO.class);
        warehouseInSkuMapper.insert(warehouseInSku);
        // 返回
        return warehouseInSku.getId();
    }

    @Override
    public void updateWarehouseInSku(WarehouseInSkuSaveReqVO updateReqVO) {
        // 校验存在
        validateWarehouseInSkuExists(updateReqVO.getId());
        // 更新
        WarehouseInSkuDO updateObj = BeanUtils.toBean(updateReqVO, WarehouseInSkuDO.class);
        warehouseInSkuMapper.updateById(updateObj);
    }

    @Override
    public void deleteWarehouseInSku(Long id) {
        // 校验存在
        validateWarehouseInSkuExists(id);
        // 删除
        warehouseInSkuMapper.deleteById(id);
    }

    private void validateWarehouseInSkuExists(Long id) {
        if (warehouseInSkuMapper.selectById(id) == null) {
            throw exception(WAREHOUSE_IN_SKU_NOT_EXISTS);
        }
    }

    @Override
    public WarehouseInSkuDO getWarehouseInSku(Long id) {
        return warehouseInSkuMapper.selectById(id);
    }

    @Override
    public PageResult<WarehouseInSkuDO> getWarehouseInSkuPage(WarehouseInSkuPageReqVO pageReqVO) {
        return warehouseInSkuMapper.selectPage(pageReqVO);
    }

    @Override
    public void deleteInWarehouseIdWarehouseInSku(Long inWarehouseId) {
        warehouseInSkuMapper.delete(WarehouseInSkuDO::getInWarehouseId,inWarehouseId);
    }

    @Override
    public List<WarehouseInSkuDO> findInWarehouseIdWarehouseInSku(Long inWarehouseId) {
        return warehouseInSkuMapper.selectList(WarehouseInSkuDO::getInWarehouseId,inWarehouseId);
    }

}